//
//  IOS_SDKViewControllerMobile.m
//  IOS_SDK
//
//  Created by Tzvi on 8/2/11.
//  Copyright 2011 STAR MICRONICS CO., LTD. All rights reserved.
//

#import "AppDelegate.h"

#import "IOS_SDKViewControllerMobile.h"
#import "StarIO/SMPort.h"
#import "PrinterFunctions.h"
#import "CheckConnectionViewController.h"
#import "BarcodeSelector.h"
#import "BarcodeSelector2D.h"
#import "Cut.h"
#import "TextFormating.h"
#import "rasterPrinting.h"
#import "StandardHelp.h"
#import "BarcodePrintingMini.h"
#import "TextFormatingMini.h"
#import "JpKnjFormating.h"
#import "JpKnjFormatingMini.h"

#import "nibLoader.h"

@implementation IOS_SDKViewControllerMobile

- (id)init
{
    self = [super init];
    if (self)
    {
    }
    return self;
}

- (IBAction)pushButtonSearch:(id)sender {
//    searchView = [[SearchPrinterViewController alloc] initWithNibName:@"SearchPrinterViewController" bundle:nil];
//    
//    [self.navigationController pushViewController:searchView animated:YES];
//    
//    [searchView release];
    [searchView removeFromSuperview];
    
    searchView = [nibLoader load:@"PrinterSearchView" params:nil];
    searchView.center = CGPointMake(480, 480);
    searchView.name_label = uilabel_portname;
    [self.view addSubview:searchView];
}

- (IBAction)pushButtonBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
                
//        arrayFunction = [[NSMutableArray alloc] init];
//        [arrayFunction addObject:@"Get Status"];
//        [arrayFunction addObject:@"Check Connection"];
//        [arrayFunction addObject:@"Sample Receipt"];
//        [arrayFunction addObject:@"JP Sample Receipt"];
//        [arrayFunction addObject:@"Begin/End Checked Block"];
//        [arrayFunction addObject:@"1D Barcodes"];
//        [arrayFunction addObject:@"2D Barcodes"];
//        [arrayFunction addObject:@"Text Formatting"];
//        [arrayFunction addObject:@"JP Kanji Text Formatting"];
//        [arrayFunction addObject:@"Raster Graphics Text Printing"];
//        [arrayFunction addObject:@"MSR"];
    }
    return self;
}

- (void)dealloc
{
//    [arrayFunction release];

    [buttonBack release];
//    [tableviewFunction release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    miniPrinterFunctions = [[MiniPrinterFunctions alloc]init];
//    
//    tableviewFunction.dataSource = self;
//    tableviewFunction.delegate   = self;
}

- (void)viewDidUnload
{
//    [miniPrinterFunctions release];
    [buttonBack release];
    buttonBack = nil;
//    [tableviewFunction release];
//    tableviewFunction = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    uilabel_portname.text = [AppDelegate getPortName];
}

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    // Return the number of sections.
//    return 1;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    // Return the number of rows in the section.
//    return [arrayFunction count];
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *CellIdentifier = @"Cell";
//    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    if (cell == nil) {
//        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//    }
//    
//    // Configure the cell...
//    int index = indexPath.row;
//    [cell.textLabel setText: [arrayFunction objectAtIndex:index]];
//    
//    return cell;
//}
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    
//    selectIndex = indexPath.row;
//    
//    UIActionSheet *actionsheetSampleReceipt = nil;
//    
//    [uitextfield_portname resignFirstResponder];
//    
//    [self SetPortInfo];
//    
//    NSString *portName = [AppDelegate getPortName];
//    NSString *portSettings = [AppDelegate getPortSettings];
//    
//    switch (selectIndex)
//    {
//        case 0:
//            [PrinterFunctions CheckStatusWithPortname:portName portSettings:portSettings];
//            break;
//            
//        case 1:
//        {
//            CheckConnectionViewController *viewController = [[CheckConnectionViewController alloc] initWithNibName:@"CheckConnectionViewController" bundle:[NSBundle mainBundle]];
//            
//            [self.navigationController pushViewController:viewController animated:YES];
//            
//            [viewController release];
//            
//            break;
//        }
//        
//        case 2:
//            actionsheetSampleReceipt = [[UIActionSheet alloc] initWithTitle:@"Printer Width" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"2 inch", @"3 inch"/*, @"4 inch"*/, nil];
//            
//            [actionsheetSampleReceipt showInView:self.view];
//            [actionsheetSampleReceipt release];
//            
//            break;
//            
//        case 3:
//            actionsheetSampleReceipt = [[UIActionSheet alloc] initWithTitle:@"Printer Width" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"2 inch", @"3 inch"/*, @"4 inch"*/, nil];
//            
//            [actionsheetSampleReceipt showInView:self.view];
//            [actionsheetSampleReceipt release];
//
//            break;
//            
//        case 4:
//            [MiniPrinterFunctions PrintCheckedBlockWithPortName:portName portSettings:portSettings widthInch:2];
//            
//            break;
//            
//        case 5:
//        {
//            BarcodePrintingMini *barcodeMini = [[BarcodePrintingMini alloc] initWithNibName:@"BarcodePrintingMini" bundle:[NSBundle mainBundle]];
//            [self.navigationController pushViewController:barcodeMini animated:YES];
//            [barcodeMini release];
//            
//            break;
//        }
//            
//        case 6:
//        {
//            BarcodeSelector2D *barcodeSelector2d = [[BarcodeSelector2D alloc] initWithNibName:@"BarcodeSelector" bundle:[NSBundle mainBundle]];
//            [self.navigationController pushViewController:barcodeSelector2d animated:YES];
//            [barcodeSelector2d release];
//            
//            break;
//        }
//            
//        case 7:
//        {
//            TextFormatingMini *textformatingvar = [[TextFormatingMini alloc] initWithNibName:@"TextFormatingMini" bundle:[NSBundle mainBundle]];
//            [self.navigationController pushViewController:textformatingvar animated:YES];
//            [textformatingvar release];
//            
//            break;
//        }
//            
//        case 8:
//        {
//            JpKnjFormatingMini *jpKnjformatingvar = [[JpKnjFormatingMini alloc] initWithNibName:@"JpKnjFormatingMini" bundle:[NSBundle mainBundle]];
//            [self.navigationController pushViewController:jpKnjformatingvar animated:YES];
//            [jpKnjformatingvar release];
//            
//            break;
//        }
//            
//        case 9:
//        {
//            rasterPrinting *rasterPrintingVar = [[rasterPrinting alloc] initWithNibName:@"rasterPrinting" bundle:[NSBundle mainBundle]];
//            [self.navigationController pushViewController:rasterPrintingVar animated:YES];
//            [rasterPrintingVar release];
//            
//            break;
//        }
//            
//        case 10:
//            [miniPrinterFunctions MCRStartWithPortName:portName portSettings:portSettings];
//            
//            break;
//    }
//}
//
//- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    [uitextfield_portname resignFirstResponder];
//    
//    [self SetPortInfo];
//    
//    NSString *portName = [AppDelegate getPortName];
//    NSString *portSettings = [AppDelegate getPortSettings];
//    
//    switch (buttonIndex)
//    {
//        case 0:
//            switch (selectIndex)
//            {
//                case 2:
//                    [MiniPrinterFunctions PrintSampleReceiptWithPortname:portName
//                                                            portSettings:portSettings
//                                                               widthInch:2];
//                    break;
//                    
//                case 3:
//                    [MiniPrinterFunctions PrintKanjiSampleReceiptWithPortName:portName
//                                                                 portSettings:portSettings
//                                                                    widthInch:2];
//                    break;
//            }
//            
//            break;
//            
//        case 1:
//            switch (selectIndex)
//            {
//                case 2:
//                    [MiniPrinterFunctions PrintSampleReceiptWithPortname:portName
//                                                            portSettings:portSettings
//                                                               widthInch:3];
//                    break;
//                    
//                case 3:
//                    [MiniPrinterFunctions PrintKanjiSampleReceiptWithPortName:portName
//                                                                 portSettings:portSettings
//                                                                    widthInch:3];
//                    break;
//            }
//            
//            break;
//        /*
//        case 2:
//            switch (selectIndex)
//            {
//                case 2:
//                    [MiniPrinterFunctions PrintSampleReceiptWithPortname:portName
//                                                            portSettings:portSettings
//                                                               widthInch:4];
//                    break;
//                    
//                case 3:
//                    [MiniPrinterFunctions PrintKanjiSampleReceiptWithPortName:portName
//                                                                 portSettings:portSettings
//                                                                    widthInch:4];
//                    break;
//            }
//            
//            break;
//         */
//    }
//}
//
//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
//}
//
//- (void)DismissActionSheet:(id)unusedID
//{
//    [tableviewFunction reloadData];
//}
//
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    if ([string isEqualToString:@"\n"] == true)
//    {
//        [uitextfield_portname resignFirstResponder];
//        return NO;
//    }
//    
//    return YES;
//}

- (IBAction)textformat:(id)sender
{
    TextFormatingMini *textformatingvar = [[TextFormatingMini alloc] initWithNibName:@"TextFormatingMini" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:textformatingvar animated:YES];
    [textformatingvar release];
}


- (IBAction)showHelp
{
    NSString *title = @"PORT PARAMETERS";
    
    NSString *helpText = [AppDelegate HTMLCSS];
    helpText = [helpText stringByAppendingString:@"<body>\
                 This program on supports ethernet cards<br/>\
                 <Code>TCP:192.168.222.244</Code><br/>\
                 <It1>Enter IP address of Star Printer</It1><br/><br/>\
                 <LargeTitle><center>Port Settings</center></LargeTitle>\
                 <p>You should leave this blank for desktop printers.  You should use 'mini' when connecting to a portable printer</p>\
                 </body><html>"];
    
    StandardHelp *helpVar = [[StandardHelp alloc]initWithNibName:@"StandardHelp" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:helpVar animated:YES];
    [helpVar release];

    [helpVar setHelpTitle:title];
    [helpVar setHelpText:helpText];
}

@end
